package com.chagee.store.operation.server.controller.pc;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.chagee.base.utils.HttpResponse;
import com.chagee.base.utils.HttpResponseUtil;
import com.chagee.base.utils.PaginationData;
import com.chagee.base.utils.PaginationRequest;
import com.chagee.store.operation.api.request.*;
import com.chagee.store.operation.api.response.SettlementResponse;
import com.chagee.store.operation.api.response.WorkOrderResponse;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.exception.BizException;
import com.chagee.store.operation.common.utils.CommonUtil;
import com.chagee.store.operation.server.facade.StoreSettlementFacade;
import com.chagee.store.operation.server.model.vo.StoreMonthOrderVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 打烊结算
 *
 * @Author : hk
 * @Date : 2024/5/10
 */

@Slf4j
@RestController
@RequestMapping("/settlement")
public class StoreSettlementController {

	@Resource
	private StoreSettlementFacade storeSettlementFacade;

	/**
	 * 供应商列表
	 *
	 * @param request
	 * @return
	 */
	@PostMapping("/querySupplier")
	@ApiOperation("分页查询供应商列表")
	public HttpResponse<PaginationData<SupplierPageQueryResponse>> querySupplier(@RequestBody @Valid PaginationRequest<QuerySupplierRequest> request) {
		log.info("StoreSettlementController_querySupplier_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		PaginationData<SupplierPageQueryResponse> data = storeSettlementFacade.querySupplier(request);
		return HttpResponseUtil.success(data);
	}

	/**
	 * 打烊结算单
	 *
	 * @param request
	 * @return
	 */
	@PostMapping("/query")
	@ApiOperation("分页查询结算单表")
	public HttpResponse<PaginationData<SettlementResponse>> query(@RequestBody PaginationRequest<SettlementRequest> request) {
		log.info("StoreSettlementController_query_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		PaginationData<SettlementResponse> data = storeSettlementFacade.query(request);
		return HttpResponseUtil.success(data);
	}

	/**
	 * 打烊结算单详情
	 *
	 * @param request
	 * @return
	 */
	@PostMapping("/settlementDetail")
	@ApiOperation("打烊结算单详情")
	public HttpResponse<SettlementResponse> settlementDetail(@RequestBody @Valid SettlementDetailRequest request) {
		log.info("StoreSettlementController_settlementDetail_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		SettlementResponse data = storeSettlementFacade.settlementDetail(request);
		return HttpResponseUtil.success(data);
	}

	/**
	 * 打烊结算单导出
	 */
	@PostMapping("/export")
	@ApiOperation("打烊结算单导出")
	public void exportStoreOpeningTask(@RequestBody SettlementRequest settlementRequest,
									   HttpServletRequest request,
									   HttpServletResponse response,
									   ModelMap map) {
		storeSettlementFacade.export(settlementRequest, request, response, map);
	}

	/**
	 * 修改门店面积
	 */
	@PostMapping("/updateStoreArea")
	@ApiOperation("修改门店面积")
	public HttpResponse<Boolean> updateStoreArea(@RequestBody @Valid UpdateStoreAreaRequest request) {
		log.info("StoreSettlementController_updateStoreArea_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return storeSettlementFacade.updateStoreArea(request);
	}

	/**
	 * 修改动线数
	 */
	@PostMapping("/updateLineNum")
	@ApiOperation("修改动线数")
	public HttpResponse<Boolean> updateLineNum(@RequestBody @Valid UpdateStoreAreaRequest request) {
		log.info("StoreSettlementController_updateLineNum_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return storeSettlementFacade.updateLineNum(request);
	}

	/**
	 * 查看工单列表
	 */
	@PostMapping("/queryOrderList")
	@ApiOperation("查看工单列表")
	public HttpResponse<PaginationData<WorkOrderResponse>> queryOrderList(@RequestBody PaginationRequest<SettlementOrderRequest> request) {
		log.info("StoreSettlementController_queryOrderList_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		return HttpResponseUtil.success(storeSettlementFacade.queryOrderList(request));
	}

	/**
	 * 工单列表导出
	 */
	@PostMapping("/orderExport")
	@ApiOperation("工单列表导出")
	public void orderExport(@RequestBody SettlementOrderRequest orderRequest,HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		log.info("StoreSettlementController_orderExport_request:{}, auth:{}", JSON.toJSONString(orderRequest), JSON.toJSONString(AuthUtil.getUser()));
		storeSettlementFacade.orderExport(orderRequest, request, response, map);
	}

	/**
	 * 结算调整
	 */
	@PostMapping("/adjust")
	@ApiOperation("结算调整")
	public HttpResponse<Boolean> settlementAdjust(@RequestBody @Valid AdjustMoneyRequest request) {
		log.info("StoreSettlementController_settlementAdjust_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return HttpResponseUtil.success(storeSettlementFacade.settlementAdjust(request));
	}

	@PostMapping("/getUploadInvoiceUrl")
	@ApiOperation("获取上传发票链接")
	public HttpResponse<String> getUploadInvoiceUrl(@RequestBody @Valid InvoiceRequest request) {
		log.info("StoreSettlementController_getUploadInvoiceUrl_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return HttpResponseUtil.success(storeSettlementFacade.getUploadInvoiceUrl(request));
	}

	@PostMapping("/invoiceCallback")
	@ApiOperation("云砺推送发票信息")
	public HttpResponse<Boolean> invoiceCallback(@RequestBody InvoiceCallbackRequest request) {
		log.info("StoreSettlementController_invoiceCallback_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		try {
			Boolean callback = storeSettlementFacade.invoiceCallback(request);
			return HttpResponseUtil.success(callback);
		} catch (BizException e) {
			return HttpResponseUtil.build(false, e.getErrorCode(), e.getErrorMsg());
		} catch (Exception e) {
			log.error("StoreSettlementController_invoiceCallback_error:{}", JSON.toJSONString(request), e);
			return HttpResponseUtil.build(false, BizErrorCode.FAIL.getErrorCode(), BizErrorCode.FAIL.getErrorMsg());
		}
	}

	@PostMapping("/issued")
	@ApiOperation("结算单下发")
	public HttpResponse<Boolean> issued(@RequestBody SettlementIssuedRequest request) {
		log.info("StoreSettlementController_issued_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		String validate = CommonUtil.validateAndWarn(request);
		if (StringUtils.isNotBlank(validate)) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), validate);
		}
		Boolean issued = storeSettlementFacade.issued(request);
		return HttpResponseUtil.success(issued);
	}

}






























